<template>
  <div class="page">
    <v-pageTitle title="部门管理"></v-pageTitle>
    <imp-panel>
      <v-pageSearch>
        <el-row style="width: 100%;">
          <el-form :inline="true">
            <el-col :span="16">
              <el-form-item label="部门">
                <el-input
                  size='small'
                  placeholder="请输入查询关键字"
                  icon="search"
                  v-model="input"
                  :on-icon-click="handleIconClick">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item style="float: right;">
                <el-button  type="danger" :disabled="this.sels.length===0" @click="del" size="small">删除</el-button>
              </el-form-item>
              <el-form-item style="float: right;">
                <el-button  type="primary" @click="batchAdd" size="small">新增部门</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </v-pageSearch>
      
      <v-pageTable pagination :sizeChange="sizeChange" :pageChange="pageChange" :paginationTotal="paginationTotal" paginationAlign="right">
        <el-table v-loading="tableData.loading" :data="tableData.body" border style="width: 100%" @selection-change="selsChange" :default-sort="{prop: 'date', order: 'descending'}">
            <el-table-column fixed type="selection" width="50">
            </el-table-column>
            <el-table-column type="index" label="序号" width="120" align="center">
            </el-table-column>
            <el-table-column prop="flag" label="部门标识" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="branch" label="部门名称" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="master" label="责任人姓名" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="100" sortable>
              <template slot-scope="scope">
                <el-tag
                  :type="scope.row.status | statusFilterType">{{scope.row.status | statusFilter}}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="120" sortable>
            </el-table-column>
            <el-table-column label="操作" fixed="right" min-width="150">
                <template slot-scope="scope">
                  <el-button  @click="batchEdit(scope.row)" type="primary" size="small">编辑</el-button>
                  <el-button  @click="handleDel(scope.$index, scope.row)" type="danger" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
      </v-pageTable>
    </imp-panel>
    <!--编辑界面-->
    <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="部门标识" prop="flag">
          <el-input type="number" v-model="ruleForm.flag"></el-input>
        </el-form-item>
        <el-form-item label="部门名称" prop="branch">
          <el-input v-model="ruleForm.branch"></el-input>
        </el-form-item>
        <el-form-item label="责任人姓名" prop="name">
          <el-select v-model="ruleForm.name" filterable placeholder="请选择查询关键字">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch on-text="" off-text="" v-model="ruleForm.status"></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容"
            v-model="ruleForm.remark">
          </el-input>
        </el-form-item>
        <el-form-item label="当前时间">
            <h3 style="font-weight:400;">{{nowDate}}</h3>
        </el-form-item>
        <el-form-item>
            <el-button  type="primary" @click.native="submitForm('ruleForm')" :loading="editLoading">提交</el-button>
            <el-button  @click.native="editFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import panel from "@/components/common/panel.vue"
import util from '../../util/util'
export default {
  components: {
    'imp-panel': panel
  },
  data() {
    return {
      input: '',
      tableData: {
          loading: true,
          head: [],
          body: []
      },
      paginationTotal: 100,
      sels:[],

      options: [
        {
          value: '选项1',
          label: '张召忠'
        }, {
          value: '选项2',
          label: '张喜'
        }, {
          value: '选项3',
          label: '陈光明'
        }, {
          value: '选项4',
          label: '许静海'
        }, {
          value: '选项5',
          label: '张家力'
        },
        {
          value: '选项6',
          label: '司琳'
        }, {
          value: '选项7',
          label: '岩栓天'
        }, {
          value: '选项8',
          label: '俗称并'
        }, {
          value: '选项9',
          label: '王盼盼'
        }, {
          value: '选项10',
          label: '王亚'
        }
      ],

      //编辑界面数据
      ruleForm: {
        flag: '',
        name: '',
        branch: '',
        post: '',
        status: false,
        remark: ''
      },
      rules: {
        flag: [
          { required: true, message: '请输入部门标识', trigger: 'blur' }
        ],
        branch: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请选择责任人', trigger: 'blur' }
        ]
      },
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,

      nowDate: util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
    }
  },
  filters: {
    statusFilter(statue) {
      let sta = statue ? 1 : 0;
      let statusMap = ['禁用','启用']
      return statusMap[sta]
    },
    statusFilterType(statue) {
      let sta = statue ? 1 : 0;
      let statusMapType = ['error','success']
      return statusMapType[sta]
    }
  },
  methods: {
    handleIconClick(){
      console.log(this.input);
    },
    getTableData(data) {
      // ajax 查询表格数据
      this.tableData.body = [
        {
            branch: '系统平台部',
            flag: '130000001',
            master: '郑山',
            remark: '物联网平台开发',
            status: true
        }, {
            branch: '移动终端部',
            flag: '130000002',
            master: '吴先锋',
            remark: 'PDA终端开发',
            status: true
        }
      ]
      // 模拟加载
      setTimeout(() => {
          this.tableData.loading = false
      }, 1000)
    },
    sizeChange(val){
        console.log(val)
        this.getTableData()
    },
    pageChange(val){
        console.log(val)
        this.getTableData()
    },
    selsChange: function (sels) {
        this.sels = sels;
    },
    handleClick(row) {
        console.log(row);
    },
    //显示新增界面
    batchAdd(){
        this.editFormVisible = true;
    },
    //显示编辑界面
    batchEdit: function (row) {
        this.editFormVisible = true;
        this.ruleForm = Object.assign({}, row);
    },
    batchClick(){
        // var ids = this.sels.map(item => item.id).toString();
        console.log(this.sels)
    },
    del: function (index, row) {
      this.$confirm('确认删除该员工吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { id: row.id };
        removeUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getUsers();
        });
      }).catch(() => {

      });
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
        if (valid) {
            alert('submit!');
            this.editFormVisible = false;
        } else {
            console.log('error submit!!');
            return false;
        }
        });
    },
    resetForm(formName) {
        this.$refs[formName].resetFields();
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该岗位吗?', '提示', {
          type: 'warning'
      }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { id: row.id };
          removeUser(para).then((res) => {
              this.listLoading = false;
              //NProgress.done();
              this.$message({
                  message: '删除成功',
                  type: 'success'
              });
              this.getUsers();
          });
      }).catch(() => {

      });
    }
  },
  watch: {
    //第一种写法 适用于普通变量（简单类型的值的观测写法）
    // 关闭dialog的时候 初始化页面表单数据
    editFormVisible: function (val, oldVal) {
      console.log('new: %s, old: %s', val, oldVal)
      if(!val){
        console.log(this.ruleForm)
        this.ruleForm = {
          flag: '',
          name: '',
          branch: '',
          post: '',
          status: false,
          remark: ''
        }
      }
    }
  },
  mounted() {
      // 获取table数据
      this.getTableData();

      let  _this = this;
      this.timer = setInterval(function(){
          _this.nowDate = util.formatDate.format(new Date(), 'yyyy-MM-dd hh:mm:ss');
      },1000);
  },
  beforeDestory(){
      if(this.timer){
          clearInterval(this.timer)
      }
  }
}
</script>